package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.bean.TurnoBean;

public class TurnoDao {

public void inserir (Connection conn, TurnoBean value) throws Exception{
		
		String sql = "INSERT INTO turno(turno) VALUES (?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, value.getDescricao());
		ps.executeUpdate();
		ps.close();
		
	}
	
	public void alterar (Connection conn, TurnoBean value) throws Exception{
		
		String sql = "UPDATE turno SET turno=? WHERE id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, value.getDescricao());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, TurnoBean value) throws Exception {

		String sql = "DELETE * FROM turno WHERE id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	public ArrayList<TurnoBean> getLista(Connection conn, TurnoBean value) throws Exception{

		String sql = "SELECT turno FROM turno where id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<TurnoBean> na = new ArrayList<TurnoBean>();
		while(rs.next()){
			TurnoBean a = new TurnoBean();
			a.setDescricao(rs.getString("turno"));
			na.add(a);
		}
		return na;
	}

	public List<TurnoBean> getLista(Connection conn) throws Exception{

		String sql = "SELECT * FROM turno t where t.id = t.id order by t.turno";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<TurnoBean> na = new ArrayList<TurnoBean>();
		TurnoBean a = null;
		while(rs.next()){
			a = new TurnoBean();
			a.setId(rs.getInt("id"));
			a.setDescricao(rs.getString("turno"));
			na.add(a);
		}
		return na;
	}	
}
